package com.example.android.chartapplication;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.RectF;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;

public class ChartGesture extends View {
    public ChartGesture(Context context) {
        super(context);
        init();
    }

    public ChartGesture(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ChartGesture(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    PointF[] initPointF = new PointF[9];
    RectF[] initRectF = new RectF[9];
    String initPassword;
    ArrayList<PointF> linkedPointF = new ArrayList<>();
    String linkedPassword = "";
    private static final float WIDTH = 300, HEIGHT = 300;

    private void init() {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                float x = WIDTH / 3 * j + WIDTH / 6;
                float y = WIDTH / 3 * i + WIDTH / 6;

                initPointF[i*3 + j] = new PointF(x, y);
                initRectF[i*3 + j] = new RectF(x - 10, y - 10, x + 10, y + 10);
                initPassword = "123";
            }
        }
    }

    Paint paint = new Paint();
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setAntiAlias(true);
        paint.setColor(Color.GRAY);

        for (PointF p : initPointF) {
            canvas.drawCircle(p.x, p.y, 20, paint);
        }
    }

    Paint contentPaint = new Paint();
    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        canvas.save();

        contentPaint.setColor(Color.BLACK);
        contentPaint.setAntiAlias(true);
        contentPaint.setStrokeWidth(6);

        float lastX = 0, lastY = 0;
        for (int i = 0; i < linkedPointF.size(); i++) {
            float x = linkedPointF.get(i).x, y = linkedPointF.get(i).y;
            canvas.drawCircle(x, y, 10, contentPaint);

            if (i >= 1) canvas.drawLine(lastX, lastY, x, y, contentPaint);
            lastX = x;
            lastY = y;
        }

        if(isMoving) {
            canvas.drawLine(lastX, lastY, moveX, moveY, contentPaint);
        }

        canvas.restore();
    }

    float moveX, moveY;

    boolean isMoving = false;
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        float x = event.getX();
        float y = event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (isMoving) break;
                for (int i = 0; i < 9; i++) {
                    if (!initRectF[i].contains(x, y)) continue;
                    linkedPointF.add(initPointF[i]);
                    linkedPassword += (i+1) + "";
                    isMoving = true;
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (!isMoving) break;
                for (int i = 0; i < 9; i++) {
                    if (!initRectF[i].contains(x, y) || linkedPointF.contains(initPointF[i])) continue;
                    linkedPointF.add(initPointF[i]);
                    linkedPassword += (i+1) + "";
                    isMoving = true;
                }
                break;
            case MotionEvent.ACTION_UP:

                Toast.makeText(getContext(), linkedPassword, Toast.LENGTH_SHORT).show();
                isMoving = false;
                new Handler().postDelayed(() -> {
                    linkedPassword = "";
                    linkedPointF.clear();
                    postInvalidate();
                }, 1000);
                break;
        }
        moveX = x;
        moveY = y;
        postInvalidate();
        return true;
    }
}
